package cn.zcj.studytest.controller;

import cn.zcj.studytest.ThrowData.Result;
import cn.zcj.studytest.domain.Image;
import cn.zcj.studytest.service.InsertContentService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin")
@Slf4j
public class InsertContentController {
    @Resource
    private InsertContentService insertContentService;

    @RequestMapping("/upload")
    public Result upload(@RequestParam MultipartFile file,@RequestParam long userId,@RequestParam Boolean isBuy){
        Result result = new Result();
        try {
            Image image = new Image();
            image.setImg(file.getBytes());
            image.setUserId(userId);
            image.setIsBuy(isBuy);
            return insertContentService.unLoadImage(image);
        }catch (Exception e){
            log.error("上传失败",e);
            result.setMsg("上传失败");
            result.setSuccess(false);
            return result;
        }
    }

    @RequestMapping("/getImage")
    public Result getImage(@RequestBody Image image){
        Result result = new Result();
        try {
            return insertContentService.getImage(image);
        }catch (Exception e){
            log.error("获取图片失败",e);

            result.setMsg("获取图片失败");
            result.setSuccess(false);
            return result;
        }

    }

    @RequestMapping("/deleteImage")
    public Result deleteImage(@RequestBody Image image){
        Result result = new Result();
        try {
            return insertContentService.deleteImg(image);
        }catch (Exception e){
            log.error("删除",e);
            result.setMsg("删除失败");
            result.setSuccess(false);
            return result;
        }
    }

    @RequestMapping("/configImg")
    public Result configImg(@RequestBody Image image){
        Result result = new Result();
        try {
            return insertContentService.configImg(image);
        }catch (Exception e){
            log.error("配置",e);
            result.setMsg("配置失败");
            result.setSuccess(false);
            return result;
        }
    }

    @RequestMapping("/getByImgList")
    public Result getByImgList(@RequestBody Image image){
        Result result = new Result();
        try {
            return insertContentService.getByImgList(image);
        }catch (Exception e){
            log.error("获取图片列表失败",e);
            result.setMsg("获取图片列表失败");
            result.setSuccess(false);
            return result;
        }

    }

    @RequestMapping("/buyImg")
    public Result buyImg(@RequestBody Image image){
        Result result = new Result();
        try {
            return insertContentService.buyImg(image);
        }
        catch (Exception e){
            log.error("购买图片失败",e);
            result.setMsg("购买图片失败");
            result.setSuccess(false);
            return result;
        }
    }

}
